//
//  ActivityDetailsViewController.m
//  豆瓣
//
//  Created by dllo on 15/11/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ActivityDetailsViewController.h"
#import "Activity.h"
#import "UIImageView+WebCache.h"
#import "LoginViewController.h"
#import <sqlite3.h>

@interface ActivityDetailsViewController ()

@end

@implementation ActivityDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
   /******************************* 活动详情界面 *********************************/
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = self.act.title;

    // 左上角返回键
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]];

    // 右上角收藏键
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_share.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
    [self.navigationItem.rightBarButtonItem setTintColor:[UIColor whiteColor]];

    // 活动名称
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, self.view.frame.size.width - 30, 50)];
    nameLabel.font = [UIFont systemFontOfSize:20];
    nameLabel.text = self.act.title;
    nameLabel.numberOfLines = 3;
    [self.view addSubview:nameLabel];
    [nameLabel release];

    // 活动图片
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(20, nameLabel.frame.origin.y + nameLabel.frame.size.height + 15, 120, 160)];
    [imageV sd_setImageWithURLStr:self.act.image];
    [self.view addSubview:imageV];
    [imageV release];

    //时间标志图片
    UIImageView *timeImageV = [[UIImageView alloc] initWithFrame:CGRectMake(imageV.frame.origin.x + imageV.frame.size.width + 12, imageV.frame.origin.y + 5, 24, 24)];
    timeImageV.image = [UIImage imageNamed:@"icon_date_blue.png"];
    [self.view addSubview:timeImageV];
    [timeImageV release];
    
    // 时间Label
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(timeImageV.frame.origin.x + timeImageV.frame.size.width + 4, timeImageV.frame.origin.y, 190, timeImageV.frame.size.height)];
    NSRange range = {5, 11};
    timeLabel.text =  timeLabel.text = [NSString stringWithFormat:@"%@ -- %@", [self.act.begin_time substringWithRange:range], [self.act.end_time substringWithRange:range]];
    [self.view addSubview:timeLabel];
    timeLabel.font = [UIFont systemFontOfSize:15];
    [timeLabel release];

    // 主办方标志图片
    UIImageView *sponsorImageV = [[UIImageView alloc] initWithFrame:CGRectMake(timeImageV.frame.origin.x, timeImageV.frame.origin.y + timeImageV.frame.size.height + 7, 25, 25)];
    sponsorImageV.image = [UIImage imageNamed:@"icon_sponsor_blue.png"];
    [self.view addSubview:sponsorImageV];
    [sponsorImageV release];
    
    // 主办方Label
    UILabel *sponsorLabel = [[UILabel alloc] initWithFrame:CGRectMake(timeLabel.frame.origin.x, sponsorImageV.frame.origin.y, timeLabel.frame.size.width, timeLabel.frame.size.height)];
    sponsorLabel.text = self.act.name;
    [self.view addSubview:sponsorLabel];
    sponsorLabel.font = [UIFont systemFontOfSize:15];
    [sponsorLabel release];

    //类型标志图片
    UIImageView *typeImageV = [[UIImageView alloc] initWithFrame:CGRectMake(timeImageV.frame.origin.x, sponsorImageV.frame.origin.y + sponsorImageV.frame.size.height + 8, 25, 25)];
    typeImageV.image = [UIImage imageNamed:@"icon_catalog_blue.png"];
    [self.view addSubview:typeImageV];
    [typeImageV release];
    
    // 类型Label
    UILabel *typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(timeLabel.frame.origin.x, typeImageV.frame.origin.y, timeLabel.frame.size.width, timeLabel.frame.size.height)];
    typeLabel.text = self.act.category_name;
    [self.view addSubview:typeLabel];
    typeLabel.font = [UIFont systemFontOfSize:15];
    [typeLabel release];

    //地址标志图片
    UIImageView *addressImageV = [[UIImageView alloc] initWithFrame:CGRectMake(timeImageV.frame.origin.x, typeImageV.frame.origin.y + typeImageV.frame.size.height + 8, 25, 25)];
    addressImageV.image = [UIImage imageNamed:@"icon_spot_blue.png"];
    [self.view addSubview:addressImageV];
    [addressImageV release];
    
    // 地址Label
    UITextView *addressLabel = [[UITextView alloc] initWithFrame:CGRectMake(timeLabel.frame.origin.x - 8, addressImageV.frame.origin.y - 4, timeLabel.frame.size.width, timeLabel.frame.size.height + 100)];
    addressLabel.text = self.act.address;
    [self.view addSubview:addressLabel];
    [addressLabel setEditable:NO];
    addressLabel.font = [UIFont systemFontOfSize:15];
    [addressLabel release];

    // 活动介绍Label
    UILabel *introduceLabel = [[UILabel alloc] initWithFrame:CGRectMake(imageV.frame.origin.x, imageV.frame.origin.y + imageV.frame.size.height + 15, 100, 50)];
    introduceLabel.text = @"活动介绍";
    introduceLabel.font = [UIFont boldSystemFontOfSize:24];
    [self.view addSubview:introduceLabel];
    [introduceLabel release];
    
    UITextView *introduceText = [[UITextView alloc] initWithFrame:CGRectMake(introduceLabel.frame.origin.x, introduceLabel.frame.origin.y + introduceLabel.frame.size.height + 3, [UIScreen mainScreen].bounds.size.width - 2 * introduceLabel.frame.origin.x, 250)];
    introduceText.text = self.act.content;
    introduceText.font = [UIFont systemFontOfSize:15];
    [introduceText setEditable:NO];
    [self.view addSubview:introduceText];
    [introduceText release];
}

// 返回活动列表界面
- (void)leftAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}

// 收藏活动
- (void)rightAction {
    
    // 打开数据库
    static sqlite3 *DB = nil;
    NSString *dbPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *filePath = [dbPath stringByAppendingPathComponent:@"dataBase.db"];
    sqlite3_open(filePath.UTF8String, &DB);
    
    // 查询有没有状态为登录("T")
    NSString *sqlStr = [NSString stringWithFormat:@"SELECT * FROM user WHERE  userState LIKE '%%%@%%'", @"T"];
    sqlite3_stmt *stmt = nil;
    sqlite3_prepare(DB, sqlStr.UTF8String, -1, &stmt, NULL);
    
    NSString *name = nil;
    
    while (SQLITE_ROW == sqlite3_step(stmt)) {
        
        const unsigned char *userName = sqlite3_column_text(stmt, 1);
        name = [NSString stringWithUTF8String:(const char *)userName];
    }
    
    if (name == nil) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"还没有登录,是否登录? " preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            [self.navigationController pushViewController:loginVC animated:YES];
            [loginVC release];
        }];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
            return;
        }];
        
        [alert addAction:yesAction];
        [alert addAction:cancelAction];
        
        [self presentViewController:alert animated:YES completion:^{
            
        }];
    }
    else {
        
        // 查找有没有收藏过该活动
        NSString *sqlStr = [NSString stringWithFormat:@"SELECT * FROM activityTable WHERE  userName LIKE '%%%@%%' and title LIKE '%%%@%%'", name, self.act.title];
        sqlite3_stmt *stmt = nil;
        sqlite3_prepare(DB, sqlStr.UTF8String, -1, &stmt, NULL);
        NSString *act = nil;
        
        while (SQLITE_ROW == sqlite3_step(stmt)) {
            
            const unsigned char *title = sqlite3_column_text(stmt, 2);
            act = [NSString stringWithUTF8String:(const char *)title];
        }
        if (nil == act) {

        // 插入活动收藏
        NSString *sqlStr1 = [NSString stringWithFormat:@"INSERT INTO  activityTable(userName, title, begin_time, end_time, address, category_name, image, name, content) VALUES ('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@')", name, self.act.title, self.act.begin_time, self.act.end_time, self.act.address, self.act.category_name, self.act.image, self.act.name, self.act.content];
        sqlite3_exec(DB, sqlStr1.UTF8String, NULL, NULL, NULL);
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:nil message:@"收藏成功啦" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil];
            
        [alter show];
        [alter release];
        return;
        }
        else {
            
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:nil message:@"已经收藏过啦" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil];
            
        [alter show];
        [alter release];
        return;
        }
    }
    sqlite3_close(DB);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
}

@end
